package sky.cin.framework.config;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import sky.cin.framework.log.*;

import java.io.File;
import java.util.Iterator;

public class XMLUtils {
    
    private final SAXReader reader = new SAXReader();
    
    private final Croog log;
    
    private Element root;
    
    private final File file;
    
    public XMLUtils(String fileName) {
        this(new File(fileName));
    }
    
    public XMLUtils(File file) {
        log = new Croog("sky.cin.framework.config.XMLUtils");
        this.file = file;
        this.load();
    }
    
    public Element getRoot() {
        return root;
    }
    
    private void load() {
        try {
            root = reader.read(file).getRootElement();
            log.info("已加载配置文件: " + file.getName());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    public Iterator childNodesIterator() {
        return root.elementIterator();
    }
    
}
